<template>
  <el-dialog :title="`编辑我的信息-${user.userName}`" :close-on-click-modal="false"
             :visible.sync="show" width="25%"
             :show-close="false">
    <el-form ref="form" :model="user" label-position="right" label-width="70px">
      <el-form-item label="头像">
        <el-upload class="avatar-uploader" action="http://localhost:5421/jvyou/upload" :show-file-list="false"
                   :on-success="handleAvatarSuccess"
                   :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="user.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="user.phoneNum" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input v-model="user.idCard" placeholder="请输入身份证号"></el-input>
      </el-form-item>
    </el-form>
    <div class="ymm-dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>

  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      user: this.$store.getters.user,
      imageUrl: this.$store.getters.user.avatar,
    }
  },
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    closeDialog() {
      this.$emit('closeDialog')
    },
    handleSubmit() {
      this.$emit('handleSubmit', this.user)
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data;
      this.user.avatar = this.imageUrl
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10MB!');
      }
      return isLt2M;
    },
  }
}
</script>

<style scoped lang="less">
/deep/ .avatar-uploader .el-upload {
  border: 2px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
